package com.example.mahao_final.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.mahao_final.entity.Blog

@Dao
interface BlogDao {
    // 插入博客数据并返回 ID
    @Insert
    suspend fun insert(blog: Blog): Long

    // 获取所有博客
    @Query("SELECT * FROM blogs")
    suspend fun getAllBlogs(): List<Blog>

    // 根据博客 ID 获取博客
    @Query("SELECT * FROM blogs WHERE id = :id")
    suspend fun getBlogById(id: Long): Blog?

    // 根据用户名获取博客
    @Query("SELECT * FROM blogs WHERE author = :username")
    suspend fun getBlogsByUsername(username: String): List<Blog>

    // 删除指定博客
    @Query("DELETE FROM blogs WHERE id = :id")
    suspend fun deleteBlogById(id: Long)

    // 更新指定博客
    @Update
    suspend fun updateBlog(blog: Blog)
}
